<?php


namespace app\admin\controller;

use think\Db;


class Announce extends Base
{
    // 公告展示
    public function index()
    {
        $where = [];
        $where['status'] = ['>', 0];
        $title = input('title');

        if (!empty($title)) {
            $where['title'] = ['like', '%' . $title . '%'];
        }

        $list = Db::name('announce')
            ->where($where)
            ->order('update_time desc')
            ->field('id, title, subtitle, img_id, content, create_time, update_time')
            ->paginate()
            ->each(
                function($item, $key){
                    $list = Db::name('image')->where('id',$item['img_id'])->find();
                    $item['url'] = $list['url'];
                    return $item;
                }
            );

        $show = $list->render();
        $show = preg_replace("(<a[^>]*page[=|/](\d+).+?>(.+?)<\/a>)", "<a href='javascript:ajax_page($1);'>$2</a>", $show);

        $this->assign('data', $list);
        $this->assign('page', $show);
        $this->assign('title', $title);

        if (request()->isAjax()) {
            return $this->fetch('ajax_index');
        } else {
            return $this->fetch();
        }
    }

    // 添加公告
    public function add()
    {
        return $this->fetch();
    }

    // 处理添加公告
    public function doAdd()
    {
        $arr = [];
        $arr['title'] = input('title');
        $arr['content'] = input('content');
        $arr['create_time'] = time();
        $arr['update_time'] = time();
        $pic = input('picture');

        if($pic){
            $img = [];
            $img['url'] = $pic;

            $food_id = Db::name('image')->insertGetId($img);

            if($food_id){
                $arr['img_id'] = $food_id;
                $info = Db::name('announce')->insert($arr);

                if($info){
                    $this->success('添加成功！', url('admin/Announce/index'));
                }else{
                    $this->error('添加失败,请重试');
                }
            }else{
                $this->error('添加失败,请重试');
            }
        }else{
            $info = Db::name('announce')->insert($arr);

            if($info){
                $this->success('添加成功！', url('admin/Announce/index'));
            }else{
                $this->error('添加失败,请重试');
            }
        }


    }

    // 修改公告
    public function edit()
    {
        $id = input('id');
        $info = Db::name('announce')
            ->alias('a')
            ->join('image i', 'a.img_id = i.id', 'left')
            ->where('a.id', $id)
            ->field('a.id, a.title, a.subtitle, a.img_id, a.content, i.url')
            ->find();
        $this->assign('info', $info);

        return $this->fetch();
    }

    // 删除公告图片
    public function delPic()
    {
        $id = input('id');
        $info = Db::name('image')->where('id',$id)->delete();

        if($info){
            $result = array(
                'status' => 1,
                'msg' => '删除成功!'
            );
        }else{
            $result = array(
                'status' => 0,
                'msg' => '删除失败!'
            );
        }

        return($result);
    }

    //新增公告图片
    public function addPic() {
        $food_id = input('food_id');
        $info = input('info/a');
        $data = [];
        foreach($info as $k=>$v){
            $data[$k]['url'] = $v;
            $data[$k]['id'] = Db::name('image')->insertGetId(['url'=>$v]);
            Db::name('announce')->where('id', $food_id)->update(['img_id'=>$data[$k]['id']]);
        }

        if($data){
            $result = array(
                'status' => 1,
                'msg' => '上传成功!',
                'arr' => $data
            );
        }else{
            $result = array(
                'status' => 0,
                'msg' => '上传失败!'
            );
        }

        return($result);
    }

    // 处理修改公告
    public function doEdit()
    {
        $id = input('id');
        $data = [];
        $data['title'] = input('title');
        $data['subtitle'] = input('subtitle');
        $data['content'] = input('content');
        $data['update_time'] = time();

        $info = Db::name('announce')->where('id', $id)->update($data);

        if ($info !== false) {
            $this->success('修改成功!', url('admin/Announce/index'));
        } else {
            $this->error('修改失败,请重试');
        }
    }

    // 删除公告
    public function del()
    {
        $id = input('id');
        $info = Db::name('announce')->where('id', $id)->delete();

        if ($info) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}